var QJAXS_ = [ ]

function GET_QJAX(id_cola, url, parametros, callback)
{
    QJAX_IMPL_(id_cola, url, parametros, callback, GET_QJAX_IMPL_)
}

function POST_QJAX(id_cola, url, parametros, callback)
{
    QJAX_IMPL_(id_cola, url, parametros, callback, POST_QJAX_IMPL_)
}

function TIMED_GET_QJAX(id_cola, url, parametros, callback, timeout)
{
    QJAX_IMPL_(id_cola, url, parametros, callback, timeout, TIMED_GET_QJAX_IMPL_)
}

function TIMED_POST_QJAX(id_cola, url, parametros, callback, timeout)
{
    QJAX_IMPL_(id_cola, url, parametros, callback, timeout, TIMED_POST_QJAX_IMPL_)
}


function QJAX_IMPL_(id_cola)
{
    if (COLA_QJAX_OCUPADA_(id_cola)) {
        return AGREGA_COLA_QJAX_(id_cola, arguments)
    }
    
    arguments[arguments.length - 1].apply(null, arguments)
}


function GET_QJAX_IMPL_(id_cola, url, parametros, callback)
{
    QJAX_SEND_(id_cola, GET_JAX_INIT_(url, true, parametros), callback, null)
}

function POST_QJAX_IMPL_(id_cola, url, parametros, callback)
{
    QJAX_SEND_(id_cola, POST_JAX_INIT_(url, true, parametros), callback, parametros)
}

function TIMED_GET_QJAX_IMPL_(id_cola, url, parametros, callback, timeout)
{
    TIMED_QJAX_SEND_(id_cola, GET_JAX_INIT_(url, true, parametros), callback, timeout, null)
}

function TIMED_POST_QJAX_IMPL_(id_cola, url, parametros, callback, timeout)
{
    TIMED_QJAX_SEND_(id_cola, POST_JAX_INIT_(url, true, parametros), callback, timeout, parametros)
}


function COLA_QJAX_OCUPADA_(id_cola)
{
    var cola = QJAXS_[id_cola]
    
    if (cola == null) {
        QJAXS_[id_cola] = [ ]
        return false
    }
    
    return (cola.length != 0)
}

function AGREGA_COLA_QJAX(id_cola, params)
{
    QJAXS_[id_cola].push(params)
}

function DESPACHA_COLA_QJAX_(id_cola)
{
    var cola = QJAXS_[id_cola]

    if (cola.length) {
        var siguiente = cola.shift( )        
        siguiente[siguiente.length - 1].apply(null, siguiente)
    }
}


function QJAX_SEND_(id_cola, qjax, callback, parametros) {
    qjax.onreadystatechange = function( ) {
        if (qjax.readyState == 4) {
            var respuesta = qjax.responseText
            
            LIBERA_JAX_(qjax)
            callback(respuesta)
            
            DESPACHA_COLA_QJAX_(id_cola)
        }
    }
    
    qjax.send(parametros)
}

function TIMED_QJAX_SEND_(id_cola, qjax, callback, timeout, parametros) {
    var timer = null
    
    qjax.onreadystatechange = function( ) {
        if (qjax.readyState == 4) {
            clearTimeout(timer)
            
            var respuesta = qjax.responseText
            LIBERA_JAX_(qjax)
            
            callback(respuesta)
            DESPACHA_COLA_QJAX_(id_cola)
        }
    }
    
    timer = setTimeout(
        function( ) {
            LIBERA_FORZADO_JAX_(qjax)
            
            callback("timeout")
            DESPACHA_COLA_QJAX_(id_cola)
        },
        timeout
    )
    
    qjax.send(parametros)
}
